package com.ecommerce.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.ecommerce.dal.service.IAdminManagerService;

@Controller
@RequestMapping("/admin/*")
@SessionAttributes({"admin"})
public class AdminController extends ParentController {
	
	@Autowired
	@Qualifier("adminValidator")
	private Validator adminValidator;
	
	@Autowired
	private IAdminManagerService adminManagerService;
	
	@RequestMapping(value="/IndexAdmin.htm", method = RequestMethod.GET)
	public String indexAdminInit(ModelMap map) {
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String name = user.getUsername();
	
		map.addAttribute("username", name);
		map.addAttribute("message", "Spring Security login + database example");
//		return "redirect:IndexAdmin.htm";
		return "IndexAdmin";
	}
	
	@RequestMapping(value="/LoginAdmin.htm", method = RequestMethod.GET)
	public String loginAdminInit(ModelMap map) {
		System.out.println("Inside loginAdminInit");
//		org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User();
		return "LoginAdmin";
	}
	
	@RequestMapping(value="/LoginFailed.htm", method = RequestMethod.GET)
	public String loginFailedAdmin(ModelMap map) {
		
		map.addAttribute("error", "true");
		return "LoginAdmin";
	}
	
	@RequestMapping(value="/LogoutAdmin.htm", method = {RequestMethod.POST, RequestMethod.GET})
	public String logoutAdmin(ModelMap map, HttpServletRequest request, SessionStatus status) {
		return "LoginAdmin";
	}
	
	@RequestMapping(value="/AddAdmin.htm", method = RequestMethod.GET)
	public String addAdminInit(ModelMap map) {
		return "AddAdmin";
	}
	
	@RequestMapping(value="/AddAdmin.htm", method = RequestMethod.GET)
	public String addAdminSubmit(ModelMap map) {
		return "AddAdmin";
	}
	
	@RequestMapping(value="/DeleteAdmin.htm", method = RequestMethod.GET)
	public String deleteAdminInit(ModelMap map) {
		return "DeleteAdmin";
	}
	
	@RequestMapping(value="/DeleteAdmin.htm", method = RequestMethod.GET)
	public String deleteAdminSubmit(ModelMap map) {
		return "DeleteAdmin";
	}
}
